<template>
  <el-dialog title="会员充值" :visible.sync="visible" :close-on-click-modal="false"
    :before-close="handleCloseDialog" width="680px">
    <el-form :model="dataObject" :rules="rules" ref="adminForm" label-width="100px" size="small">
      <el-form-item label="会员：" prop="phoneNo">
        <el-input v-model="dataObject.phoneNo" disabled placeholder="请输入会员"  clearable></el-input>
      </el-form-item>
      <el-form-item label="余额：" prop="balance">
        <el-input v-model="dataObject.balance" disabled placeholder="请输入余额" clearable></el-input>
      </el-form-item>
       <el-form-item label="操作类型：" prop="type">
          <el-select v-model="dataObject.type" placeholder="请选择操作类型">
            <el-option v-for="item in rechargeOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>
       <el-form-item label="金额：" prop="amount">
        <el-input v-model="dataObject.amount" type="number" placeholder="请输入金额" clearable></el-input>
      </el-form-item>
    
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleConfirmDialog('adminForm')" size="mini" :loading="loading">确 定</el-button>
      <el-button @click="handleCloseDialog" size="mini">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addMemberMoney,reduceMemberMoney } from '@/api/member/index';
import { createNamespacedHelpers } from "vuex";
const { mapState } = createNamespacedHelpers("member");
const defaultObject = {
  balance: "",
  phoneNo: "",
  type:'1',
  amount:''
};

export default {
  data() {
    return {
      visible:true,
      loading: false,
      dataObject: Object.assign({}, defaultObject)
    };
  },
  props: {
    row: {
      type: Object,
      default: {}
    }
  },
  computed: {
    ...mapState({
      rules: state => state.rules,
      rechargeOptions: state => state.routes.rechargeOptions
    })
  },
  mounted() {
     Object.keys(this.dataObject).forEach(key => {
      if(key!=='type'){
        this.dataObject[key] = this.row[key];
      }
    });
  },
  methods: {
    handleCloseDialog() {
      this.$emit("handle-cancel");
    },
    handleConfirmDialog(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.loading=true;

          if(this.dataObject.type==='1'){
             addMemberMoney({amount:this.dataObject.amount,id:this.row.id}).then(response => {
                this.resetForm(formName);
                this.$message({
                  message: "增加成功",
                  type: "success",
                  duration: 1000
                });
                this.loading=false;
                this.$emit("handle-confirm"); 
              }).catch(()=>{
                this.loading=false;
              });

              return;
          }
          reduceMemberMoney({amount:this.dataObject.amount,id:this.row.id}).then(response => {
              this.resetForm(formName);
              this.$message({
                message: "减少成功",
                type: "success",
                duration: 1000
              });
              this.loading=false;
              this.$emit("handle-confirm"); 
            }).catch(()=>{
              this.loading=false;
            });
          
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dataObject = Object.assign({}, defaultObject);
    }
  }
};
</script>
<style scoped>
</style>